import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Multiplayer implements ActionListener{
	private ArrayList<String> tempNames;
	private ArrayList<Integer> tempTokens;
	private JFrame frame;
	private JPanel panel;
	private ImageIcon iiMultiplayer,AddPlayer,Start;
	private JLabel labelMultiplayer,NarutoName,SakuraName,SasukeName,KakashiName,NejiName,HinataName,ItachiName,OrochimaruName;
	private JLabel naruto,sasuke,sakura,kakashi,itachi,orochimaru,neji,hinata,error ;
	private ImageIcon iinaruto,iisasuke,iisakura,iikakashi,iiitachi,iiorochimaru,iineji,iihinata ;
	private JLabel[] label;
	private JTextField[] playerField;
	private JButton AddPlayerButton;
	private JButton StartButton;
	private String[] tokens = {"Naruto","Sasuke","Sakura","Orochimaru","Hinata","Kakashi","Neji","Itachi"};
	private JComboBox[] tokenBox;
	private int y, players=2;
	private textBased monopolyLogic;
	private boolean isAcceptableTokens=false, isAcceptableNames=false;
	private monopolyGUI gui;
	private monopolySolo solo;
	private boolean tempBool;
	
	public Multiplayer() {
		this.initComponents();
	}
	
	public void initComponents(){
		frame = new JFrame("Monopoly");
		panel = new JPanel();
		AddPlayerButton = new JButton();
		AddPlayer = new ImageIcon("images/button/addplayer.png");
		Start = new ImageIcon("images/button/startbutton.png");
		AddPlayerButton.setIcon(AddPlayer);
		error = new JLabel();
		naruto = new JLabel();
		sasuke = new JLabel();
		sakura = new JLabel();
		kakashi = new JLabel();
		orochimaru = new JLabel();
		hinata = new JLabel();
		itachi = new JLabel();
		neji = new JLabel();
		NarutoName = new JLabel("Naruto");
		SakuraName = new JLabel("Sakura");
		SasukeName = new JLabel("Sasuke");
		KakashiName = new JLabel("Kakashi");
		ItachiName = new JLabel("Itachi");
		OrochimaruName = new JLabel("Orochimaru");
		NejiName = new JLabel("Neji");
		HinataName = new JLabel("Hinata");
		StartButton = new JButton();
		StartButton.setIcon(Start);
		iiMultiplayer = new ImageIcon("images/player select.jpg");
		iinaruto = new ImageIcon("images/tokens/down-naruto.gif");
		iisasuke = new ImageIcon("images/tokens/down-sasuke.gif");
		iisakura = new ImageIcon("images/tokens/down-sakura.gif");
		iiorochimaru = new ImageIcon("images/tokens/down-ochimaru.gif");
		iikakashi = new ImageIcon("images/tokens/down-kakashi.gif");
		iineji = new ImageIcon("images/tokens/down-neji.gif");
		iihinata = new ImageIcon("images/tokens/down-hinata.gif");
		iiitachi = new ImageIcon("images/tokens/down-itachi.gif");
		naruto.setIcon(iinaruto);
		sasuke.setIcon(iisasuke);
		sakura.setIcon(iisakura);
		itachi.setIcon(iiitachi);
		kakashi.setIcon(iikakashi);
		neji.setIcon(iineji);
		hinata.setIcon(iihinata);
		orochimaru.setIcon(iiorochimaru);
		error.setBounds(400, 90, 300, 20);
		naruto.setBounds(500, 130, 40, 50);
		sakura.setBounds(600, 130, 40, 50);
		sasuke.setBounds(500, 210, 40, 50);
		kakashi.setBounds(600, 210,40,50);
		neji.setBounds(500, 290,40,50);
		hinata.setBounds(600, 290,40,50);
		orochimaru.setBounds(500, 370,40,50);
		itachi.setBounds(600, 370,40,50);
		NarutoName.setBounds(500,125,60,10);
		SakuraName.setBounds(600,125,60,10);
		SasukeName.setBounds(500,205,60,10);
		KakashiName.setBounds(600,205,60,10);
		NejiName.setBounds(500,285,60,10);
		HinataName.setBounds(600,285,60,10);
		ItachiName.setBounds(600,365,60,10);
		OrochimaruName.setBounds(495,365,70,10);
		panel.add(error);
		panel.add(NarutoName);
		panel.add(ItachiName);
		panel.add(SakuraName);
		panel.add(SasukeName);
		panel.add(KakashiName);
		panel.add(HinataName);
		panel.add(NejiName);
		panel.add(OrochimaruName);
		panel.add(sakura);
		panel.add(neji);
		panel.add(hinata);
		panel.add(itachi);
		panel.add(orochimaru);
		panel.add(naruto);
		panel.add(sasuke);
		panel.add(kakashi);
		labelMultiplayer = new JLabel(iiMultiplayer);
		label = new JLabel[8];
		for (int i = 0; i < 8; i++) {
			label[i] = new JLabel("Player " + (i+1));
		}
		tokenBox = new JComboBox[8];
		for (int i = 0; i < tokenBox.length; i++) {
			tokenBox[i] = new JComboBox(tokens);
		}
		playerField = new JTextField[8];
		for (int i = 0; i < 8; i++) {
			playerField[i] = new JTextField(20);
			playerField[i].setText("");
		}
		panel.setLayout(null);
		AddPlayerButton.setBounds(300, 50, 115, 30);
		AddPlayerButton.setBorder(null);
		AddPlayerButton.setBorderPainted(false);
		AddPlayerButton.setContentAreaFilled(false);
		AddPlayerButton.setOpaque(false);
		AddPlayerButton.setRolloverIcon(new ImageIcon("images/button/addplayer1.png"));
		StartButton.setBounds(420, 50, 60, 30);
		StartButton.setBorder(null);
		StartButton.setBorderPainted(false);
		StartButton.setContentAreaFilled(false);
		StartButton.setOpaque(false);
		StartButton.setRolloverIcon(new ImageIcon("images/button/startbutton1.png"));
		labelMultiplayer.setBounds(0,0,702,500);
		AddPlayerButton.addActionListener(this);
		AddPlayerButton.setActionCommand("add");
		StartButton.addActionListener(this);
		StartButton.setActionCommand("start");
		y=90;
		for (int i = 0; i < 8; i++) {
			y=y+40;
			label[i].setBounds(150, y, 200, 20);
		}
		y=90;
		for (int i = 0; i < 8; i++) {
			y=y+40;
			playerField[i].setBounds(200, y, 100, 20);
			if(i>1)
				playerField[i].setEnabled(false);
		}
		y=90;
		for (int i = 0; i < 8; i++) {
			y=y+40;
			tokenBox[i].setBounds(320, y, 90, 20);
			if(i>1)
				tokenBox[i].setEnabled(false);
		}
		for (int i = 0; i < 8; i++) {
			panel.add(playerField[i]);
		}
		for (int i = 0; i < 8; i++) {
			panel.add(label[i]);
		}
		for (int i = 0; i < 8; i++) {
			panel.add(tokenBox[i]);
		}
		panel.add(AddPlayerButton);
		panel.add(StartButton);
		panel.add(labelMultiplayer);
	}
	
	public void createAndShowGUI(boolean temp){
		this.tempBool = temp;
		frame.add(panel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(200, 150, 710, 534);
		frame.setVisible(true);
	}
	
	public void getNames(){
		tempNames = new ArrayList<String>();
		int counter=0;
		for (int i = 0; i < players; i++) {
			tempNames.add(String.valueOf(playerField[i].getText()));
			if(String.valueOf(playerField[i].getText()).equals("")){
				counter++;
			}
		}
		if(counter==0){
			isAcceptableNames=true;
		}
	}

	public void getTokens() {
		tempTokens = new ArrayList<Integer>();
		int counter=0;
		Integer[] checkToken;
		checkToken = new Integer[players];
		for (int i = 0; i < players; i++) {
			tempTokens.add(tokenBox[i].getSelectedIndex());
			checkToken[i] = tokenBox[i].getSelectedIndex();
		}
		for (int i = 0; i < players; i++) {
			for (int k = i+1; k < players; k++) {
				if(checkToken[i] == checkToken[k]){
					counter++;
				}
			}
		}
		if(counter==0){
			isAcceptableTokens=true;
		}
	}

	public void actionPerformed(ActionEvent e) {
		if("add".equals(e.getActionCommand())){
			tokenBox[players].setEnabled(true);
			playerField[players].setEnabled(true);
			if(players<7){
				players++;
			}
			else{
				players++;
				AddPlayerButton.setEnabled(false);
			}
		}
		if("start".equals(e.getActionCommand())){
			this.getNames();
			this.getTokens();
			if(isAcceptableTokens && isAcceptableNames){
				monopolyLogic = new textBased();
				monopolyLogic.createPlayers(players, tempNames);
				monopolyLogic.chooseToken(tempTokens);
				if(tempBool){
					gui = new monopolyGUI(monopolyLogic.getPlayer(), monopolyLogic.getProperties());
					gui.createAndShowGUI();
				}
				else{
					solo = new monopolySolo(monopolyLogic.getPlayer(), monopolyLogic.getProperties());
					solo.createAndShowGUI();
				}
				this.frame.setVisible(false);
			}
			else{
				isAcceptableNames = false;
				isAcceptableTokens = false;
				error.setEnabled(true);
				error.setText("Please Fill out all the requirements.");
			}
		}
	}
}